<?php


namespace App\Services\Tencent;


use App\Services\Configs\DefaultService;

class BaseService
{
    public function __construct($configKey)
    {
        $config = $this->getConfig($configKey);
        $this->secretId = $config['secretId'] ?? null;
        $this->secretKey = $config['secretKey'] ?? null;
        $this->appid = $config['appId'] ?? null;
        $this->region = $config['region'] ?? null;
        $this->privateBucket = 'privateceshi-1257723087'; //私有桶
        $this->publicBucket = 'publicceshi-1257723087';//公开桶
    }

    /**
     * 获取配置信息
     * @param $key 资源标识
     * @return mixed 数组
     * @throws \Exception
     * @author an
     * @time 2021-12-06 14:42
     */
    protected function getConfig($key)
    {
        $config = new DefaultService();
        $data = $config->first($key);
        if (!$data) {
            throw new \Exception('找不到配置 ' . $key);
        }
        return $data;
    }

    /**
     * http 请求
     * @param $url 接口路径
     * @param $bodys 参数
     * @param $headers 请求头
     * @param string $method 请求方法
     * @return bool|string 请求结果
     * @author an
     * @time 2021-12-06 14:41
     */
    protected function request($url, $bodys, $headers, $method = 'POST')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$" . $url, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $data = curl_exec($curl);
        return $data;
    }
}
